/**
 * 推广记录明细
 * author wim
 */
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/entity/address_entity.dart';
import 'package:panda/entity/common_entity.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/SignUtil.dart';
import '../base/Constant.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class TuiGuangMingXiPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TuiGuangMingXiView();
  }
}

class TuiGuangMingXiView extends State<TuiGuangMingXiPage> {
  List<CommonEntityDataData> dataList = new List();
  int page = 1;
  String token = "";
  EasyRefreshController _controller = EasyRefreshController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      loadList();
    });
  }

  loadList() async {
    Map map = new Map();
    map['token'] = token;
    map['p'] = page;
    map['per'] = 10;
    map['sign_type'] = SignType.GET_RECHARGE_INFO;
    Response<String> response =
        await HttpUtil().post(Api.GET_RECHARGE_INFO, data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    CommonEntityEntity homeListEntity = new CommonEntityEntity.fromJson(userMap);
    if (mounted)
      setState(() {
        if (page == 1) {
          dataList.clear();
          dataList.addAll(homeListEntity.data.datas);
          _controller.finishRefresh();
          _controller.resetLoadState();
        } else {
          dataList.addAll(homeListEntity.data.datas);
          _controller.finishLoad(noMore:homeListEntity.data.datas.length==0);
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);

    return new Scaffold(
      appBar: new AppBar(
        title: Text('账单明细',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black)),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        elevation: 0.5,
      ),
      body: new Container(
        color: divideColor,
        child: new EasyRefresh(
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          header: MaterialHeader(),
          footer: MaterialFooter(),
          controller: _controller,
          child: ListView.separated(
              itemBuilder: _renderRow,
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  color: divideColor,
                  height: 1,
                );
              },
              itemCount: dataList.length),
          onRefresh: () async {
            page = 1;
            loadList();
          },
          onLoad: () async {
            page++;
            loadList();
          },
        ),
      ),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    return new GestureDetector(
      child: new Container(
        color: Colors.white,
        padding: EdgeInsets.all(15.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Text(
                    dataList[index].action_zh,
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                        color: Colors.black),
                  ),
                ),
                Text(
                   dataList[index].action_symbol+dataList[index].money,
                  style: TextStyle(
                      fontSize:
                          ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                      color: Colors.black87),
                ),
              ],
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  0, ScreenUtil.getInstance().setHeight(DP_10), 0, 0),
              child: Text(
                dataList[index].create_time,
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                    color: Colors.black54),
              ),
            ),
          ],
        ),
      ),
      onTap: () {},
    );
  }
}
